package com.bdqn.reflect;

import com.bdqn.reflect.pojo.Person;

import java.lang.reflect.Modifier;

/**
 * 测试1-获得类的信息
 *
 * @author LILIBO
 * @since 2024/6/27
 */
public class Test01ClassInfo {

    public static void main(String[] args) {
        // 方式一：通过类获取Calss对象
        Class clazz01 = Person.class;
        System.out.println("类的简单名称：clazz.getSimpleName() = " + clazz01.getSimpleName());
        System.out.println("类的完整名称：clazz.getName() = " + clazz01.getName());
        System.out.println("该类父类名称：clazz.getSuperclass().getSimpleName() = " + clazz01.getSuperclass().getSimpleName());
        System.out.println("该类实现的所有接口：clazz.getInterfaces()\n--------");
        for (Class iclz : clazz01.getInterfaces()) {
            System.out.println(iclz.getSimpleName());
        }
        System.out.println("--------");

        // 方式二：通过对象获取到类的Class对象
        Person hongshuai = new Person("美华"); // 创建一个对象：美华
        Class clazz02 = hongshuai.getClass();
        System.out.println("该类所在的包：clazz.getPackage() = " + clazz02.getPackage());

        // 方式三（重要）：通过类的名称（字符串）获取类的Class对象
        String className = "com.bdqn.reflect.pojo.Person";
        try {
            Class clazz03 = Class.forName(className);
            int modifiers = clazz03.getModifiers();
            if (modifiers == Modifier.PUBLIC) {
                System.out.println("类的访问修饰符：clazz.getModifiers() = PUBLIC " + modifiers);
            }
        } catch (ClassNotFoundException e) {
            e.fillInStackTrace();
        }

    }

}
